package com.mew.game.strategy;

import com.mew.common.dc.db.GameRoomService;
import com.mew.common.dc.redis.AucRedisService;
import com.mew.common.entity.config.Room;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Slf4j
public class RoomProfileDataSupport {

    @Autowired
    private GameRoomService gameRoomService;
    @Autowired
    private DefaultRedisScript<Long> strategyScript;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private AucRedisService aucRedisService;

    /**
     * @param gameId
     * @param roomId
     * @param betNumber
     * @param paymentNumber
     * @return
     */
    public long persistentProfile(long userId, int gameId, int roomId, long betNumber, long paymentNumber){

        Room room = gameRoomService.getRedisRoom(gameId, roomId);
        if (null == room){
            log.error("Room is close gameId {} roomId {}", gameId, roomId);
            return 1;
        }



       long res =  redisTemplate.execute(strategyScript, Arrays.asList(String.valueOf(roomId)), betNumber, paymentNumber);

        if (res == -1 || res == 0){
            log.error("Room Blood Error gameId {} roomId {} res {}", gameId,  room.getId(), res);
            return res;
        }

       return res;

    }

    public void persistentPlayInfo(long userId, long goldNumber, long betNumber, long paymentNumber, int multiple){
        aucRedisService.incrementPayment(userId, goldNumber, betNumber, paymentNumber, multiple);
    }


}
